package com.taotao.controller;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.taotao.common.pojo.EUDataGridResult;
import com.taotao.common.pojo.ItemParamResult;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.pojo.TbItemParam;
import com.taotao.pojo.TbItemParamItem;
import com.taotao.service.ItemParamService;
import com.taotao.utils.JsonUtils;

@Controller
public class ItemParamController {
	
	@Autowired
	private ItemParamService itemParamService;
	
	@RequestMapping(value="/item/param/list")
	@ResponseBody
	public EUDataGridResult getItemParamList(int page, int rows) {
		return itemParamService.getItemParamList(page, rows);
	}
	
	@RequestMapping(value="/item/param/query/itemcatid/{catId}")
	@ResponseBody
	public TaotaoResult checkItemCatId(@PathVariable int catId) {
		return itemParamService.checkCatId(catId);
		
	}
	
	@RequestMapping(value="/item/param/save/{catId}", method=RequestMethod.POST)
	@ResponseBody
	public TaotaoResult saveItemParam(@PathVariable long catId, String paramData) {
		TbItemParam param = new TbItemParam();
		param.setItemCatId(catId);
		param.setParamData(paramData);
		param.setCreated(new Date());
		param.setUpdated(new Date());
		itemParamService.saveItamParam(param);
		return TaotaoResult.ok(param);
		
	}
	
	@RequestMapping(value="/item/{itemId}/param", method=RequestMethod.GET)
	public ModelAndView getItemParamByItemId(@PathVariable long itemId) {
		ModelAndView mav = new ModelAndView("testItemParam");
		TbItemParamItem itemParamItem = itemParamService.getItemParamByItemId(itemId);
		List<ItemParamResult> list = JsonUtils.jsonToList(itemParamItem.getParamData(), ItemParamResult.class);
		mav.addObject("list", list);
		return mav;
		
	}
}
